// #include<stdio.h>
// #include<string.h>
#include"edit_file.h"

/*******************************************************************
*    obj_buf: 解析目标字符串
*    obj_title:查询解析目标字符串中字符串
*    MSG_sit: 目标字符所在位置
*******************************************************************/

void getMSG(char *obj_buf,char *obj_title,int MSG_sit)
{
   char *ret;
   int i=0;
    if(strstr(obj_buf,obj_title)!=NULL)
    {
        //分割字符串
		ret=strtok(obj_buf,",");
		while((ret=strtok(NULL,","))!=NULL)
		{
			i++;
            if(i==MSG_sit) 
            {
                sprintf(ret,"%s,",ret);
                strcat(OutData_buff,ret);
            }
		}
    }
}

/*****************************************************
* 将文件解析，导出解析文件

*****************************************************/
void edit_F(char *file_addr,int hand)
{
    char buff[255];//存储每次读取文件的字符串内容
    //char OutData_buff[50];
    FILE *fp=NULL;
    FILE *fp1=NULL;
    char *ret;
    int i;
   // fp=fopen("./20211015/PASS/DUT0_085254_AL142K000085.csv","r");
    fp=fopen(file_addr,"r");
    if(fp==NULL)
    {
        printf("目标文件打开失败\n");
    }
    fp1=fopen("OutData.csv","a+");
    if(fp1==NULL)
    {
        printf("输出文件打开失败\n");
    }
    if(hand==0)
    {
        sprintf(buff,"File Name,Power On Current(mA),Charge Current(mA),BatteryVoltage_Low(V),BatteryVoltage_High(V),Idle Current(mA),Pmu Idle Current(mA),Power Off(uA),\n");
        fputs(buff,fp1);
    }

    strcat(OutData_buff,file_name); //将解析文件名称写入编辑缓存中
    strcat(OutData_buff,",");       //文件名称后加分格；

    while(!feof(fp))
	{
		i=0;
        memset(buff,0,255);
		fgets(buff,255,(FILE*)fp);
        //printf("输出内容>%s\n",buff);

        getMSG(buff,"Power On Current(mA)",1);
        getMSG(buff,"Charge Current(mA)",1);
        getMSG(buff,"BatteryVoltage_Low(V)",1);
        getMSG(buff,"BatteryVoltage_High(V)",1);
        getMSG(buff,"Idle Current(mA)",1);
        getMSG(buff,"Pmu Idle Current(mA)",1);
        getMSG(buff,"Power Off(uA)",1);
       
    }

    strcat(OutData_buff,"\n");
    fputs(OutData_buff,fp1);
    memset(OutData_buff,0,50);
    fclose(fp);
    fclose(fp1);

    printf(">文件编辑完成<\n");

}